<?php 
    //订单页面控制器
    class OrderAction extends Action{
        public function index(){
            
        }
        
        //加载订单内容
        public function loadOrder(){
            $UserId=1;
            $Orders=handleOrder(getOrders($UserId));
            $this->assign('oList1',splitOrder($Orders,1))
                 ->assign('oList2',splitOrder($Orders,2))
                 ->assign('oList3',splitOrder($Orders,3))
                 ->assign('oList4',splitOrder($Orders,4))
                 ->display('Order');
            
            
        }
        
        //取消订单
        public function cancelOrder(){
            $OrderId=$_POST['isOrderId'];
            //echo $OrderId;
            $r1=M('order')->where("OrderId='$OrderId'")->delete();
            
            foreach (getOrderItem($OrderId) as $key => $value) {
                updateProInfo($value['ProId'],-intval($value['ProCount']),1);
            }
            
            $r2=M('order_item')->where("OrderId='$OrderId'")->delete();
            
            //p($OrderId);
            //p($r2);
            
            $result=0;
            if($r1>0 && $r2>0){
                $result=1;
            }
            $this->ajaxReturn($result);
            
        }
        
        //确认收货
        public function conReceive(){
            $OrderId=$_POST['isOrderId'];
            //echo $OrderId;
            $data['OrderState']=4;
            $data['conDate']=time();
            $result=M('order')->where("OrderId='$OrderId'")->save($data);
            
            $this->ajaxReturn($result);
        }
        
       //订单数
       public function getCount(){
           $uId=$_POST['UserId'];
           $result=array();
           for ($i=1; $i <=4; $i++) { 
               array_push($result,count(M('order')->where("UserId='$uId' and OrderState='$i'")->select()));
           }
           
           $this->ajaxReturn($result);
           
       }
       
       //商品评价
       public function proValuation(){
           //获取参数
           $oId=$_POST['isOrderId'];
           $uId=$_POST['isUserId'];
           $pId=$_POST['isProId'];
           $data['VContent']=$_POST['isContent'];
           $data['VDate']=time();
           $data['Vlevel']=$_POST['isLevel'];
           $data['VName']=M('user')->where("UserId='$uId'")->select()[0]['Name'];
           $data['ProId']=$pId;
           //判断是否已评价
           //p($pId);
           //$this->ajaxReturn(1);
           if(checkOrderV($oId,$pId)!=0){
               $this->ajaxReturn(1);
           }
           //p('test');
           //写评论
           if(M('pro_valuation')->add($data)<=0)
                $this->ajaxReturn(2);
           //修改评价状态
           $aData['ynValuation']=1;
           if(M('order_item')->where("OrderId='$oId' and ProId='$pId'")->save($aData)<=0)
                $this->ajaxReturn(3);
           
           $this->ajaxReturn(4);
           //echo $oId.$vc;
       }
       
       //创建订单
        public function nowPay(){
            
            //读取参数
            $userId=$_POST['UserId'];
            $pId=$_POST['ProId'];
            $pCount=$_POST['ProCount'];
            
            $address=M('user_info')->where("UserId='$userId' and YnDefault='ui-icon-checked-s'")->select();
            
            if(getProCount($pId)<$pCount){//检测库存
                $this->ajaxReturn(33);
            }
            
            if(count($address)>0){
                $data['UserName']=$address[0]['TakeName'];
                $data['UserPhone']=$address[0]['TakePhone'];
                $data['Address']=$address[0]['TakeAddress'];
            }
            
            //订单
            $data['OrderState']=1;
            $data['CreateDate']=time();
            $data['UserId']=$userId;
            $data['OrderId']=time()*10+$userId;
            $data['Total']=getProducts($pId)[0]['ProPrice']*$pCount;
            
            //p($data);
            
            $result=M('order')->add($data);
            if($result<=0){
                $this->ajaxReturn(0);
            }
            
            //订单细目
            $data2['OrderId']=$result;
            $data2['ProId']=$pId;
            $data2['ProCount']=$pCount;
            
            //保存并判断
            if(M('order_item')->add($data2)<=0){
                $this->ajaxReturn(0);
            }
            
            //更新商品销售信息
            $os=getOrderItem($result);
            //p($os[0]['ProId']);
            foreach ($os as $key => $value) {
                updateProInfo($value['ProId'],$value['ProCount'],1);
            }
            
            //返回结果
            $this->ajaxReturn($result);
        }
    }
?>